package com.flyxiaozhu.erp.core.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * @author flyxiaozhu
 * @date 2019-04-12 0:17
 */
@Entity
@Data
@DynamicInsert
@DynamicUpdate
@JsonIgnoreProperties({"handler", "hibernateLazyInitializer"})
//@SQLDelete(sql = "update goods set is_deleted = 1 where id = ?")
//@Where(clause = "is_deleted = 0")
@Table(indexes = {
        @Index(columnList = "name"),
        @Index(columnList = "goodsNo"),
        @Index(columnList = "categoryId"),
        @Index(columnList = "status"),
        @Index(columnList = "examineStatus")
})
public class Goods extends BaseEntity {

    @Column(columnDefinition = "varchar(255) not null default '' comment '商品名称'")
    private String name;

    @Column(columnDefinition = "varchar(32) not null default '' comment '商品编号'")
    private String goodsNo;

    @Column(columnDefinition = "tinyint(2) not null default 1 comment '发货规则，1实物'")
    private int ruleId;

    @Column(columnDefinition = "tinyint(2) not null default 1 comment '商品类型，1实物'")
    private int type;

    @Column(columnDefinition = "int(11) not null default 0 comment '市场价格'")
    private int marketPrice;

    @Column(columnDefinition = "int(11) not null default 0 comment '销售价格'")
    private int sellingPrice;

    @Column(columnDefinition = "int(11) not null default 0 comment '商品分类'")
    private long categoryId;

    @Column(columnDefinition = "varchar(255) not null default '' comment '缩略图'")
    private String thumb;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(columnDefinition = "text comment '商品详情'")
    private String detail;

    @Column(columnDefinition = "int(11) not null default 0 comment '销量'")
    private int sales;

    @Column(columnDefinition = "tinyint(2) not null default 0 comment '状态，0上架，1下架'")
    private int status;

    @Column(columnDefinition = "tinyint(2) not null default 0 comment '审核状态，0待审核，1审核通过，2审核失败'")
    private int examineStatus;

    @Column(columnDefinition = "int(11) not null default 0 comment '总库存'")
    private int stock;

    @Column(columnDefinition = "tinyint(1) not null default 0 comment '是否含有多SKU'")
    private Boolean hasMultiSku;

    @Column(columnDefinition = "TIMESTAMP COMMENT '更新时间'")
    private String updateTime;

    //    @JsonIgnore // 序列化时忽略该字段里面的递归
    @ManyToMany
    @JoinTable(name = "goods_tags", joinColumns = @JoinColumn(name = "goods_id"),
            inverseJoinColumns = @JoinColumn(name = "tags_id"))
//    @Fetch(FetchMode.SUBSELECT) List中使用
    private List<GoodsTag> tags = new ArrayList<>();

    // orphanRemoval=true 删除无关联数据,cascade = CascadeType.ALL 否则插入的新的多方的数据会为空
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    @JoinColumn(name = "goods_id")
//    @Fetch(FetchMode.SUBSELECT)// 抓取策略 会另外发送一条select语句抓取前面查询到的所有实体对象的关联实体 防止查询结果结果字段重复
    private List<GoodsPicture> goodsPictures = new ArrayList<>();

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    @JoinColumn(name = "goods_id")
    private List<GoodsExamineLog> examineLogs = new ArrayList<>();

    @JsonIgnore
    @Column(columnDefinition = "tinyint(2) not null default 0 comment '是否删除，0否，1是'")
    private Boolean isDeleted;

//    不使用 OneToMany 因为获取SKU是实时的，导致添加新SKU无法获取被删除的SKU数据
//    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
//    @JoinColumn(name = "goods_id")
////    @Fetch(FetchMode.SUBSELECT)
//    private List<GoodsSku> goodsSkus = new ArrayList<>();


    @Transient
    List<GoodsAttrName> attrs;

    @Transient
    List<GoodsSku> goodsSkus;
}
